﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game.MiniGame.Puzzle
{
    class Piece
    {
        private const int WIDTH = 420;
        private const int HEIGHT = 360;
        
        /// <summary>
        /// 조각으로 자를 원본 이미지
        /// </summary>
        private Texture2D PuzzleImage;

        private Rectangle SourceRectangle;

        private Rectangle DestinationRectangle;

        private Rectangle SolutionRectangle;

        private bool IsCorrect = false;


        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="PuzzleNo">퍼즐위치번호</param>
        /// <param name="image">원본이미지</param>
        /// <param name="row">행</param>
        /// <param name="column">열</param>
        public Piece(int PuzzleNo, Texture2D image, int row, int column)
        {
            int width = WIDTH / column; //열 하나의 width
            int height = HEIGHT / row;  //행 하나의 height
            int x = (PuzzleNo % 4) * width;
            int y = (PuzzleNo / 4) * height;

            PuzzleImage = image;

            SourceRectangle = new Rectangle(x, y, width, height);
            DestinationRectangle = new Rectangle(x + 190, y + 60, width, height);
            //???????? 깊은 복산지 얕은 복산지???
            SolutionRectangle = DestinationRectangle;
        }

        /// <summary>
        /// 정답을 확인하는 함수
        /// </summary>
        public bool CheckLocation()
        {
            if (DestinationRectangle.X == SolutionRectangle.X && 
                DestinationRectangle.Y == SolutionRectangle.Y)
            {
                IsCorrect = true;
            }

            return IsCorrect;
        }

        /// <summary>
        /// 인자로 들어온 Rectangle과 현재 Rectangle을 바꿔주는 것
        /// </summary>
        /// <param name="ChangeRectangle">바꿔치기 할 Rectangle</param>
        public void MovePiece(Rectangle ChangeRectangle)
        {
            Rectangle Tmp = ChangeRectangle;
            ChangeRectangle = DestinationRectangle;
            DestinationRectangle = Tmp;
        }

        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(PuzzleImage, DestinationRectangle, Color.White);
        }
    }
}